'use strict';

const Controller = require('egg').Controller;
const fs = require("fs")
const path = require("path")
class CarController extends Controller {
	// 车辆信息发布并存入数据库
	async Carinformation() {
		const {
			ctx
		} = this;
		var ziduan = ctx.request.body
		// console.log(ziduan.Liveaction);
		// 传的图片
		var f1 = ctx.request.files
		// console.log(f1);
		var files1 = "";
		var files2 = "";
		// console.log(f1.length);
		for (var i = 0; i < f1.length; i++) {
			if (f1[i].fieldname == "files") {
				// console.log(f1[i].fieldname, "666666666666666666");

				let oldpath = f1[i].filepath
				let newpath = __dirname + "/../public/releaseCar/img/" + path.basename(oldpath)
				fs.copyFile(oldpath, newpath, (err) => {})
				ziduan[f1[i].field] = "/public/releaseCar/img/" + path.basename(oldpath)
				files1 = files1 + "---" + ziduan[f1[i].field]+","
			} else {
				let oldpath = f1[i].filepath
				let newpath = __dirname + "/../public/releaseCar/img/" + path.basename(oldpath)
				fs.copyFile(oldpath, newpath, (err) => {})
				ziduan[f1[i].field] = "/public/releaseCar/img/" + path.basename(oldpath)
				files2 = files2 + "---" + ziduan[f1[i].field]+","
			}

		}

		let sql =
			`INSERT INTO carinformation (shift, carname, peopel, price, VehicleType,displacement,skylight,door,fuel,volume,VehicleCar,Liveaction) VALUES("${ziduan.shift}", "${ziduan.name}","${ziduan.peopel}", "${ziduan.price}","${ziduan.VehicleType}","${ziduan.displacement}","${ziduan.skylight}","${ziduan.door}","${ziduan.fuel}","${ziduan.volume}","${files1}","${files2}")`
		let result = await this.app.mysql.query(sql)
		this.ctx.body = {
			info: "发布成功",
			code: 2001

		}
	}

}


module.exports = CarController;
